package org.goyo.main.web.controller.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.goyo.common.baseBean.AjaxResult;
import org.goyo.common.baseBean.Constants;
import org.goyo.common.exception.DBException;
import org.goyo.common.exception.ServiceException;
import org.goyo.common.utils.JSONUtil;
import org.goyo.common.utils.Pager;
import org.goyo.common.utils.ToolUtils;
import org.goyo.main.domain.entity.BaseObject;
import org.goyo.main.domain.entity.base.Warehouse;
import org.goyo.main.services.base.IWarehouseService;
import org.goyo.main.services.log.MethodLog;
import org.goyo.main.web.controller.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 
 * @author liu
 *
 */
@Controller
@RequestMapping("/warehouse")
public class WarehouseController extends BaseController {
  protected Logger logger = LoggerFactory.getLogger(this.getClass());

  @Resource
  private IWarehouseService warehouseService;

  /**
   * 列表界面
   * 
   * @author chenchuan 2014年11月9日
   */
  @RequestMapping("/toWarehouseList")
  public String toWarehouseList() {
    logger.info("----------------------");
    return "/base/WarehouseList";
  }

  /**
   * 保存或者更新
   * 
   * @author chenchuan 2014年11月9日
   */
  @MethodLog("保存或者更新仓库信息")
  @RequestMapping("/saveOrUpdateWarehouse")
  @ResponseBody
  public AjaxResult saveOrUpdateWarehouse(HttpServletRequest request) {
    AjaxResult result = null;
    try {
      String warehouse = request.getParameter("warehouse");
      Warehouse warehouseBean = JSONUtil.json2Obj(warehouse, Warehouse.class);
      BaseObject obj = warehouseService.saveOrUpdateInfo(warehouseBean);
      result = AjaxResult.resultSuccess("保存成功", obj);
    } catch (DBException | ServiceException e) {
      result = AjaxResult.resultFail("保存失败", null);
		logger.error(e.getMessage(),e);
    }
    return result;
  }

  /**
   * 删除
   * 
   * @author chenchuan 2014年11月9日
   */
  @MethodLog("删除仓库信息")
  @RequestMapping("/delWarehouse")
  @ResponseBody
  public AjaxResult delWarehouse(HttpServletRequest request) {
    AjaxResult result = null;
    try {
      String rows = request.getParameter("rows");
      JSONArray jsonarray = JSONArray.fromObject(rows);
      List<Warehouse> list = (List<Warehouse>) JSONArray.toCollection(jsonarray, Warehouse.class);
      warehouseService.delByUuids(ToolUtils.getFiles(list, "uuid").toArray());
      result = AjaxResult.resultSuccess("删除成功", null);
    } catch (Exception e) {
		logger.error(e.getMessage(),e);
      result = AjaxResult.resultFail("删除失败", null);
    }
    return result;
  }

  /**
   * 查看
   * 
   * @author chenchuan 2014年11月9日
   */
  @MethodLog("查看仓库信息")
  @RequestMapping("/viewWarehouse")
  public String viewWarehouse(HttpServletRequest request, Model model) {
    String uuid = request.getParameter("uuid");
    Warehouse info = null;
    try {
      info = warehouseService.findByUuid(uuid);
    } catch (Exception e) {
      logger.error(e.getMessage(), e);
    }
    model.addAttribute("info", JSONUtil.obj2Json(info));
    model.addAttribute("status", Constants.VIEW);
    return "/base/WarehouseEdit";
  }

  /**
   * 修改
   * 
   * @author chenchuan 2014年11月9日
   */
  @MethodLog("修改仓库信息")
  @RequestMapping("/alterWarehouse")
  public String alterWarehouse(HttpServletRequest request, Model model) {
    String uuid = request.getParameter("uuid");
    Warehouse info = null;
    try {
      info = warehouseService.findByUuid(uuid);
    } catch (Exception e) {
		logger.error(e.getMessage(),e);
    }
    model.addAttribute("info", JSONUtil.obj2Json(info));
    model.addAttribute("status", Constants.EDIT);
    return "/base/WarehouseEdit";
  }

  /**
   * 添加
   * 
   * @author chenchuan 2014年11月9日
   */
  @MethodLog("新增仓库信息")
  @RequestMapping("/addWarehouse")
  public String addWarehouse(HttpServletRequest request,Model model) {
    model.addAttribute("number",getBaseNumber(Warehouse.BIZTYPE));
    return "/base/WarehouseEdit";
  }

  /**
   * list列表数据加载
   * 
   * @author chenchuan 2014年11月9日
   */
  @MethodLog("查询仓库信息")
  @RequestMapping("/listData")
  @ResponseBody
  public String ListData(HttpServletRequest request) {
    JSONObject data = null;
    // 获取条件
    // Map<String, Object> cond = JSONUtil.json2Map(request.getParameter("cond"));
    // 分页条件
    int page = Integer.parseInt(request.getParameter("page"));
    int pageSize = Integer.parseInt(request.getParameter("pagesize"));
    String sortname = request.getParameter("sortname");
    String sortorder = request.getParameter("sortorder");

    String where = request.getParameter("where");
    // 分页器
    Pager pager = Pager.convert(page, pageSize, sortname, sortorder);
    try {
      pager = warehouseService.findByCondition(where, pager);
      Map<String, Object> map = new HashMap<String, Object>();
      map.put("Rows", pager.getList());
      map.put("Total", pager.getCount());
      data = JSONObject.fromObject(map);
    } catch (Exception e) {
		logger.error(e.getMessage(),e);
    }
    return data.toString();
  }
}
